home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15686 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: prodigy.com!usenet
  2. From: XKWR65B@prodigy.com (Mark Rubelmann)
  3. Newsgroups: comp.lang.c++
  4. Subject: HELP! .PCXes and Null Pointer error...
  5. Date: 6 Apr 1996 21:36:10 GMT
  6. Organization: Prodigy Services Company  1-800-PRODIGY
  7. Distribution: world
  8. Message-ID: <4k6o4a$1m5a@usenetp1.news.prodigy.com>
  9. NNTP-Posting-Host: innugap8-int.news.prodigy.com
  10. X-Newsreader: Version 1.2
  11.  
  12. I'm having trouble with this function to load PCX files. It compiles fine 
  13. but crashes when I try to run it. One time as I was messing around with 
  14. it, it sort of worked. All it did was show a bunch of crap on the screen 
  15. when it tried to draw it and when the program was done running it said 
  16. "Null pointer assignment." Here's the data structure for the PCX file and 
  17. two functions to allocate the memory and load it:
  18.  
  19. typedef struct pcx_picture_typ
  20. {
  21.     pcx_header header;      // 128 byte header
  22.     RGB_color palette[256]; // The palette
  23.     char far *buffer;       // Decompressed image
  24. } pcx_picture, *pcx_picture_ptr;
  25.  
  26.  
  27. int PCX_Init(pcx_picture_ptr image)
  28. {
  29.     if(!(image->buffer = (char far *)malloc(64001)))
  30.         return 0;
  31.     else
  32.         return 1;
  33. }
  34.  
  35. void PCX_Load(char *filename, pcx_picture_ptr image)
  36. {
  37.     FILE *fp;
  38.     int num_bytes,index;
  39.     long count=0;
  40.     unsigned char data;
  41.     char far *temp_buffer;
  42.  
  43.     fp = fopen(filename, "rb");  // Open the file
  44.  
  45.     temp_buffer = (char far *)image; //Load header
  46.     for(index=0; index<128; index++)
  47.         temp_buffer[index] = getc(fp);
  48.  
  49.     while(count<=ScrWidth * ScrHeight) // Load data and decompress it
  50.     {
  51.         data = getc(fp);        // Get first piece of data
  52.         if(data>=192 && data>=255) // Is it RLE?
  53.         {
  54.             num_bytes=data-192; // How many bytes in run?
  55.             data = getc(fp);    // Get data for run
  56.             while(num_bytes-->0)  // Replicate data in buffer num_bytes 
  57. times
  58.             {
  59.                 image->buffer[count++] = data;
  60.             }
  61.         }
  62.         else
  63.         {
  64.             image->buffer[count++] = data; // Just copy data into buffer
  65.         }
  66.     }
  67.  
  68.     fseek(fp,-768L,SEEK_END);   // Go to the start of the palette data
  69.     for(index=0;index<256;index++) // Load the palette
  70.     {
  71.         image->palette[index].red = (getc(fp) >> 2); // Get red value
  72.         image->palette[index].green = (getc(fp) >> 2); // Get green
  73.         image->palette[index].blue = (getc(fp) >> 2); // Get blue
  74.     }
  75.  
  76.     fclose(fp); // Close the file
  77. }
  78.  
  79. I believe the problem is that the data just isn't getting stored in image.
  80. buffer like it's supposed to. Please help!
  81.  
  82.